package com.epro.dx.activity.messagecenter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.task.DeleteNoticesAsyncTask;
import com.epro.dx.util.AsyncTaskUtil;
import com.epro.dx.widget.MyMessageSettingsSwitch;
import com.epro.dx.widget.MyMessageSettingsSwitch.MySwitchOnclickListenner;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.MessageServiceProvider;
import com.weipu.common.util.AlertToast;
import com.weipu.common.util.StringUtil;

/************************
 * @author Tony
 * @Date:2016-4-6下午5:02:59
 * @描述:消息中心页面
 ************************/
public class MessageCenterSettingsActivity extends BaseFragmentActivity {

	private MyMessageSettingsSwitch messageSwitch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sendScreen();
		//设置页面及标题部分
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_message_center_settings);
		setLeftBtnContent(true, -1);
		setTitleContent(R.string.page_title_message_center_settings);
		
		initView();
		initData();
	}

	/**
	 * @创建：Tony 2016-5-4上午9:45:14
	 * @描述：初始化也看显示数据
	 */
	private void initData() {
		String isShowEventMessage = StringUtil.getConfig(ServerConstant.MessageCenter.ISSHOWEVENTNOTICE, "true");
		messageSwitch.setSwitchState("true".equals(isShowEventMessage)?true:false);
	}

	/**
	 * @创建：Tony 2016-4-6下午5:06:42
	 * @描述：初始化视图控件
	 */
	private void initView() {
		messageSwitch = (MyMessageSettingsSwitch) findViewById(R.id.message_settings_event_swicth);
		messageSwitch.setSwitchOnclickListenner(new MySwitchOnclickListenner() {
			
			@Override
			public void onMySwitchClick() {
				//保存设置状态
				StringUtil.saveConfig(ServerConstant.MessageCenter.ISSHOWEVENTNOTICE, String.valueOf(messageSwitch.getSwitchState()));
				sendGAEvent("点击活动通知开关"+messageSwitch.getSwitchState());
			}
		});
		
		findViewById(R.id.message_settings_clear).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//弹窗提示是否清除所有消息
				showAlertDialogForClearAllMessages();
			}
		});
	}
	private AlertDialog dialog;
	/**
	 * @创建：Tony 2016-5-3下午5:09:38
	 * @描述：弹窗提示是否清除所有消息
	 */
	protected void showAlertDialogForClearAllMessages() {
		// TODO 
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.title_clear)
		.setMessage(R.string.clear_all_messages);
		builder.setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 取消
				dialog.dismiss();
			}
		});
		builder.setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				sendGAEvent("点击确认清除所有消息按钮");
				// 确认执行清除所有消息逻辑
				dialog.dismiss();
				//调用接口清除所有的消息
				AsyncTaskUtil.executeTask(new DeleteNoticesAsyncTask(MessageCenterSettingsActivity.this,MessageServiceProvider.ACTIONTYPE_ALL, null, new MyAsyncTaskListenner() {
					
					@Override
					public void onPreTask() {
						showLoadingLayout(null);
					}
					
					@Override
					public void onPostTask(String result, Object obj) {
						if(!isFinishing()){
							hideAllLayout();
							if("0".equals(result)){
								//将未读消息置为0
								StringUtil.saveConfig(ServerConstant.MessageCenter.UNREAD_MESSAGE_COUNT, "0");
								StringUtil.saveConfig(ServerConstant.MessageCenter.UNREAD_MESSAGE, String.valueOf(false));
							}else{
								AlertToast.alert(MessageCenterSettingsActivity.this, ServerConstant.ReturnCode.MESSAGEMAP.get(result));
							}
						}
						
					}
				}));
			}
		});
		
		dialog = builder.create();
		dialog.show();
	}
}
